//
//  OrderDetailController.swift
//  ExpressDriver
//
//  Created by sheng on 2017/11/9.
//  Copyright © 2017年 sheng. All rights reserved.
//

import UIKit
import MapKit
import SnapKit

class OrderDetailController: UIViewController {
    
    var mainScrollView: UIScrollView!
    
    var orderNoLabel: UILabel!
    
    var statuImage: UIImageView!
    
    var horizontalLine: UIView!
    
    var billingLabel: UILabel!
    
    var statuLabel: UILabel!
    
    var addressLabel: UILabel!
    
    var orderNumberLabel: UILabel!
    
    var remarkButton: UIButton!
    
    var itemButton: UIButton!
    
    var horizontalLine2: UIView!
    
    var trackLabel: UILabel!
    
    var mapView: MKMapView!
    
    var scrrenWidth = UIScreen.main.bounds.width
    
    var contentHeight: CGFloat!
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        setUI()
    }
    
    
    func setUI() {
        mainScrollView = UIScrollView()
        mainScrollView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
        self.view.addSubview(mainScrollView)
        
        orderNoLabel = UILabel()
        orderNoLabel.text = "451584355"
        orderNoLabel.textColor = UIColor.black
        orderNoLabel.font = UIFont.systemFont(ofSize: 16)
        mainScrollView.addSubview(orderNoLabel)
        
        orderNoLabel.snp.makeConstraints { (make) in
            make.top.equalTo(mainScrollView.snp.top).offset(15)
            make.centerX.equalTo(self.view)
        }
        
        statuImage = UIImageView(image:UIImage(named:"ic_launcher_round"))
        statuImage?.layer.borderColor = UIColor.gray.cgColor
        statuImage?.layer.borderWidth = 1.0
        mainScrollView.addSubview(statuImage)
        statuImage.snp.makeConstraints { (make) in
            make.top.equalTo(orderNoLabel.snp.bottom).offset(10)
            make.centerX.equalTo(self.view)
            make.height.equalTo(60)
            make.width.equalTo(60)
        }
        
        
        horizontalLine =  UIView()
        horizontalLine.backgroundColor = UIColor.lightGray
        mainScrollView.addSubview(horizontalLine)
        horizontalLine.snp.makeConstraints { (make) -> Void in
            make.top.equalTo(statuImage.snp.bottom).offset(10)
            make.height.equalTo(0.5)
            make.left.equalTo(10)
            make.right.equalTo(-10)
            make.centerX.equalTo(self.view)
        }
        
        
        billingLabel = UILabel()
        billingLabel.text = "Billing Information"
        billingLabel.textColor = UIColor.black
        billingLabel.font = UIFont.systemFont(ofSize: 16)
        mainScrollView.addSubview(billingLabel)
        
        billingLabel.snp.makeConstraints { (make) in
            make.top.equalTo(horizontalLine.snp.bottom).offset(5)
            make.left.equalTo(mainScrollView.snp.left).offset(10)
        }
        
        
        statuLabel = UILabel();
        statuLabel = UILabel()
        statuLabel.text = "Urgent"
        statuLabel.textColor = UIColor.red
        statuLabel.font = UIFont.systemFont(ofSize: 16)
        mainScrollView.addSubview(statuLabel)
        statuLabel.snp.makeConstraints { (make) in
            make.top.equalTo(horizontalLine.snp.bottom).offset(5)
            make.right.equalTo(mainScrollView.snp.right).offset(-10)
        }
        
        
        addressLabel = UILabel()
        addressLabel.numberOfLines = 0
        addressLabel.lineBreakMode = NSLineBreakMode.byWordWrapping
        addressLabel.text = "Billing address1、2、3, price"
        addressLabel.textColor = UIColor.black
        addressLabel.font = UIFont.systemFont(ofSize: 16)
        mainScrollView.addSubview(self.addressLabel)
        
        addressLabel.snp.makeConstraints { (make) in
            make.top.equalTo(billingLabel.snp.bottom).offset(2)
            make.left.equalTo(mainScrollView.snp.left).offset(40)
            make.right.equalTo(mainScrollView.snp.right).offset(-10)
        }
        
        
        orderNumberLabel = UILabel()
        orderNumberLabel.numberOfLines = 0
        orderNumberLabel.lineBreakMode = NSLineBreakMode.byCharWrapping
        orderNumberLabel.text = "Order No, Third party number"
        orderNumberLabel.font = UIFont.systemFont(ofSize: 16)
        mainScrollView.addSubview(orderNumberLabel)
        orderNumberLabel.snp.makeConstraints { (make) in
            make.top.equalTo(addressLabel.snp.bottom).offset(2)
            make.left.equalTo(addressLabel.snp.left)
            make.right.equalTo(mainScrollView.snp.right).offset(-10)
        }
        
        
        
        remarkButton = UIButton()
        remarkButton.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        remarkButton.setTitle("Remarks", for: UIControlState())
        remarkButton.setTitleColor(UIColor.blue, for: UIControlState())
        //self.remarkButton.addTarget(self, action: #selector(viewRemark), for: .touchUpInside)
        mainScrollView.addSubview(remarkButton)
        remarkButton.snp.makeConstraints { (make) in
            
            make.top.equalTo(orderNumberLabel.snp.bottom).offset(10)
            make.right.equalTo(mainScrollView.snp.right).offset(-15)
            make.height.equalTo(15)
        }
        
        
        itemButton = UIButton()
        itemButton.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        itemButton.setTitle("View Items", for: UIControlState())
        itemButton.setTitleColor(UIColor.blue, for: UIControlState())
        //self.itemButton.addTarget(self, action: #selector(viewItem), for: .touchUpInside)
        mainScrollView.addSubview(itemButton)
        itemButton.snp.makeConstraints { (make) -> Void in
            make.top.equalTo(orderNumberLabel.snp.bottom).offset(10)
            make.right.equalTo(remarkButton.snp.left).offset(-15)
            make.height.equalTo(15)
        }
        
        
        horizontalLine2 =  UIView()
        horizontalLine2.backgroundColor = UIColor.lightGray
        mainScrollView.addSubview(horizontalLine2)
        horizontalLine2.snp.makeConstraints { (make) -> Void in
            make.top.equalTo(itemButton.snp.bottom).offset(10)
            make.height.equalTo(0.5)
            make.left.equalTo(10)
            make.right.equalTo(-10)
            make.centerX.equalTo(self.view)
        }
        
        
        trackLabel = UILabel()
        trackLabel.text = "Tracking deatil"
        trackLabel.textColor = UIColor.black
        trackLabel.font = UIFont.systemFont(ofSize: 16)
        mainScrollView.addSubview(trackLabel)
        trackLabel.snp.makeConstraints { (make) in
            make.top.equalTo(horizontalLine2.snp.bottom).offset(5)
            make.left.equalTo(mainScrollView.snp.left).offset(10)
        }
        
        
        mapView = MKMapView()
        mapView.frame = CGRect(x: 0, y: 0, width: scrrenWidth, height: 250)
        mapView.mapType = .standard
        mapView.isScrollEnabled = true
        mapView.isZoomEnabled = true
        mainScrollView.addSubview(mapView)
        mapView.snp.makeConstraints { (make) in
            make.top.equalTo(trackLabel.snp.bottom).offset(10)
            make.size.equalTo(CGSize(width: scrrenWidth, height: 250))
        }
        
        contentHeight = orderNoLabel.frame.height + statuImage.frame.height + horizontalLine.frame.height + billingLabel.frame.height
            + addressLabel.frame.height + orderNumberLabel.frame.height + itemButton.frame.height + horizontalLine2.frame.height
            + trackLabel.frame.height + mapView.frame.width + CGFloat(79)
        
        
        mainScrollView.contentSize.width = scrrenWidth
        //mainScrollView.contentSize.height = contentHeight
        mainScrollView.isScrollEnabled = true
        mainScrollView.showsVerticalScrollIndicator = true
        mainScrollView.indicatorStyle = .black
        mainScrollView.alwaysBounceVertical = true
        mainScrollView.alwaysBounceHorizontal = false
        
        print("order lab 1 : \(orderNoLabel.frame.height)")
        print("statuImage 1 : \(statuImage.frame.height)")
        
        
        print("total height:\(contentHeight!)")
        print("contentSize height:\(mainScrollView.contentSize.height)")
    }
    
    
    
    override func viewDidAppear(_ animated: Bool) {

      
        contentHeight = orderNoLabel.frame.height + statuImage.frame.height + horizontalLine.frame.height + billingLabel.frame.height
            + addressLabel.frame.height + orderNumberLabel.frame.height + itemButton.frame.height + horizontalLine2.frame.height
            + trackLabel.frame.height + mapView.frame.height + 79
        
        
        mainScrollView.contentSize.width = scrrenWidth
        mainScrollView.contentSize.height = contentHeight
        
        print("total height 2:\(contentHeight!)")
        print("contentSize height 2:\(mainScrollView.contentSize.height)")
    }
    
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
}
